/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.mobile.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.mobile.entity.MobileMap;
import com.lczyfz.istep.modules.mobile.service.MobileMapService;

/**
 * 移动地图Controller
 *
 * @author hpp
 * @version 2017-03-29
 */
@Controller
@RequestMapping(value = "${adminPath}/mobile/mobileMap")
public class MobileMapController extends BaseController {

    @Autowired
    private MobileMapService mobileMapService;

    @ModelAttribute
    public MobileMap get(@RequestParam(required = false) String id) {
        MobileMap entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = mobileMapService.get(id);
        }
        if (entity == null) {
            entity = new MobileMap();
        }
        return entity;
    }

    @RequiresPermissions("mobile:mobileMap:view")
    @RequestMapping(value = {"list", ""})
    public String list(MobileMap mobileMap, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<MobileMap> page = mobileMapService.findPage(new Page<MobileMap>(request, response), mobileMap);
        model.addAttribute("page", page);
        return "modules/mobile/mobileMapList";
    }

    @RequiresPermissions("mobile:mobileMap:view")
    @RequestMapping(value = "form")
    public String form(MobileMap mobileMap, Model model) {
        model.addAttribute("mobileMap", mobileMap);
        return "modules/mobile/mobileMapForm";
    }

    @RequiresPermissions("mobile:mobileMap:edit")
    @RequestMapping(value = "save")
    public String save(MobileMap mobileMap, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, mobileMap)) {
            return form(mobileMap, model);
        }
        mobileMapService.save(mobileMap);
        addMessage(redirectAttributes, "保存移动地图成功");
        return "redirect:" + Global.getAdminPath() + "/mobile/mobileMap/?repage";
    }

    @RequiresPermissions("mobile:mobileMap:edit")
    @RequestMapping(value = "delete")
    public String delete(MobileMap mobileMap, RedirectAttributes redirectAttributes) {
        mobileMapService.delete(mobileMap);
        addMessage(redirectAttributes, "删除移动地图成功");
        return "redirect:" + Global.getAdminPath() + "/mobile/mobileMap/?repage";
    }

}